package com.rpframework.module.common.service.impl;

import com.rpframework.module.common.bottom.entity.enums.SourceType;
import com.rpframework.module.common.config.Config;
import com.rpframework.module.common.domain.UserLogin;
import com.rpframework.module.common.service.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.util.List;
import java.util.UUID;


/**
 * redis存数据
 *
 * @author 刘兴
 */
public class UserTokenServiceImpl implements UserTokenService {
    /**
     * redis 来源与令牌分割符号
     */
    private static final String sourceTokenSplit = "<--->";

    @Autowired
    private Config config;

    @Autowired
    private RedisTemplate<Serializable, Serializable> redisTemplate;

    @Override
    public String addToken(final UserLogin entity) {
        return redisTemplate.execute(new RedisCallback<String>() {
            String token = UUID.randomUUID().toString();

            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.set(redisTemplate.getStringSerializer().serialize(getKey(token, entity.getSource().name())),
                        redisTemplate.getStringSerializer().serialize(entity.getUserId()));
                return token;
            }
        });
    }

    /**
     * 存入redis唯一key
     *
     * @param token  用户令牌
     * @param source 登陆来源
     * @return key
     */
    public String getKey(String token, String source) {
        return config.settingKey("token", source + sourceTokenSplit + token);
    }

    @Override
    public String checkToken(final String token, final SourceType source) {
        return redisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] key = redisTemplate.getStringSerializer().serialize(getKey(token, source.name()));

                if (connection.exists(key)) {
                    byte[] value = connection.get(key);
                    return redisTemplate.getStringSerializer().deserialize(value);
                }
                return null;
            }
        });
    }

    @Override
    public List<UserLogin> allLoginDevice(String userId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean removeToken(final UserLogin token) {
        return redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                connection.del(redisTemplate.getStringSerializer()
                        .serialize(getKey(token.getToken(), token.getSource().name())));
                return true;
            }
        });
    }
}
